package by.android.khan;

import java.util.List;

import org.json.JSONObject;

import by.android.khan.adapter.PlaylistAdapter;
import by.android.khan.adapter.PlaylistCursorAdapter;
import by.android.khan.content.ContentManager;
import by.android.khan.content.bo.Playlist;
import by.android.khan.content.contract.PlaylistContract;
import by.android.khan.http.*;
import by.android.khan.service.HttpService;
import by.android.khan.task.common.ParamCallback;
import by.android.khan.R;


import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class GuestLoginActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

	private ListView mListView;
	private ProgressBar mProgressBar;
	private List<JSONObject> mListPlaylists;
	private PlaylistAdapter mPlaylistAdapter;
	private PlaylistCursorAdapter mPlaylistCursorAdapter;
	private BroadcastReceiver mHttpServiceReceiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guest_login);
		mHttpServiceReceiver = new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				if (action.equals(HttpService.ACTION_ON_ERROR)) {
					Toast.makeText(GuestLoginActivity.this, intent.getStringExtra(HttpService.EXTRA_KEY_MESSAGE), Toast.LENGTH_SHORT).show();
				} else if (action.equals(HttpService.ACTION_ON_SUCCESS)) {
					
				}
			}
		};
		IntentFilter filter = new IntentFilter();
		filter.addAction(HttpService.ACTION_ON_ERROR);
		registerReceiver(mHttpServiceReceiver, filter);
		onStartService(mListView);
		mPlaylistCursorAdapter = new PlaylistCursorAdapter(this, null);
		mListView.setAdapter(mPlaylistCursorAdapter);
		getSupportLoaderManager().initLoader(0, null, this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_guest_login, menu);
		return true;
	}

	public void onStartService(View view) {
        Intent intent = new Intent(this, HttpService.class);
        HttpRequestBuilder httpRequestBuilder = new HttpRequestBuilder();
        httpRequestBuilder.setType(HttpRequestBuilder.Type.GET);
        httpRequestBuilder.setUrl(ContentManager.PLAYLISTS_URL);
                intent.putExtra(HttpService.HTTP_REQUEST_BUILDER, httpRequestBuilder);
                startService(intent);
    }
	
	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		return new CursorLoader(this, PlaylistContract.PlaylistsColumns.CONTENT_URI,
                null, null,
                 null, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> paramLoader, Cursor paramID) {
		mPlaylistCursorAdapter.swapCursor(paramID);
		
	}

	@Override
	public void onLoaderReset(Loader<Cursor> paramLoader) {
		mPlaylistCursorAdapter.swapCursor(null);
		
	}

}
